from django import forms
from apps.basic.models import MyUser, ReadRecord
from django.core.exceptions import ValidationError
import re

# 手机号码验证函数
def mobile_validate(value):
    mobile_re = re.compile(
        r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
    if not mobile_re.match(value):
        raise ValidationError('手机号码格式错误')

# 用户信息注册表单
class UserRegForm(forms.Form):
    SEX = ((0, '男'), (1, '女'))
    STATUS = ((0, '正常'), (1, '异常'))

    username = forms.CharField(label="用户名", min_length=6,
                               widget=forms.widgets.TextInput(
                                   # 其中class样式为form-control，这是bootstrap的样式
                                   attrs={'class': 'form-control', 'placeholder': "请输入用户名"}),
                               error_messages={
                                   'required': '用户姓名不能为空',
                                   'min_length': '长度最少6位',
                               })
    password = forms.CharField(label="密码", min_length=6, max_length=10,
                               widget=forms.widgets.PasswordInput(
                                   # render_value=True，页面校验不通过后，页面上该值还存在
                                   attrs={"class": "form-control"}, render_value=True),
                               error_messages={
                                   'max_length': '密码最长10位',
                                   'required': '密码不能为空',
                                   'min_length': '密码最少6位'
                               })
    re_password = forms.CharField(label="确认密码", min_length=6, max_length=10,
                                  widget=forms.widgets.PasswordInput(
                                      # render_value=True，页面校验不通过后，页面上该值还存在
                                      attrs={"class": "form-control"}, render_value=True),
                                  error_messages={
                                      'max_length': '密码最长10位',
                                      'required': '密码不能为空',
                                      'min_length': '密码最少6位'
                                  })
    email = forms.EmailField(label="邮箱",
                             widget=forms.widgets.EmailInput(
                                 attrs={'class': 'form-control', }),
                             error_messages={
                                 'required': '邮箱不能为空',
                                 ' ': '邮箱格式不对',
                             })
    true_name = forms.CharField(label="真实姓名", widget=forms.widgets.TextInput(
                                 attrs={'class': 'form-control', }))

    mobile = forms.CharField(label="手机号码", validators=[mobile_validate],
                             widget=forms.widgets.TextInput(
                                 attrs={'class': 'form-control', }),
                             error_messages={
                                 'required': '手机号码不能为空',
                             })

    sex = forms.ChoiceField(label='性别', choices=SEX, widget=forms.widgets.Select(attrs={"class": "form-control"}),
                            error_messages={
                                'required': '性别不能为空',
                            })

    user_img = forms.ImageField(label="用户头像", required=False, widget=forms.widgets.FileInput(
                            attrs={'class': 'form-control'}))

    # 全局钩子函数
    def clean(self):
        password = self.cleaned_data.get("password")
        re_password = self.cleaned_data.get("re_password")
        if password != re_password:
            # raise forms.ValidationError("二次密码输入不一致")
            self.add_error("re_password", ValidationError("二次密码输入不一致"))


# 用户密码修改表单
class UserPassWordChangeForm(forms.Form):

    old_password = forms.CharField(label="原始密码", min_length=6, max_length=10,
                               widget=forms.widgets.PasswordInput(
                                   # render_value=True，页面校验不通过后，页面上该值还存在
                                   attrs={"class": "form-control"}, render_value=True),
                               error_messages={
                                   'max_length': '密码最长10位',
                                   'required': '密码不能为空',
                                   'min_length': '密码最少6位'
                               })
    new_password = forms.CharField(label="新密码", min_length=6, max_length=10,
                                  widget=forms.widgets.PasswordInput(
                                      # render_value=True，页面校验不通过后，页面上该值还存在
                                      attrs={"class": "form-control"}, render_value=True),
                                  error_messages={
                                      'max_length': '密码最长10位',
                                      'required': '密码不能为空',
                                      'min_length': '密码最少6位'
                                  })
    re_new_password = forms.CharField(label="确认新密码", min_length=6, max_length=10,
                                  widget=forms.widgets.PasswordInput(
                                      # render_value=True，页面校验不通过后，页面上该值还存在
                                      attrs={"class": "form-control"}, render_value=True),
                                  error_messages={
                                      'max_length': '密码最长10位',
                                      'required': '密码不能为空',
                                      'min_length': '密码最少6位'
                                  })
    # 全局钩子函数
    def clean(self):
        new_password = self.cleaned_data.get("new_password")
        re_new_password = self.cleaned_data.get("re_new_password")
        if new_password != re_new_password:
            # raise forms.ValidationError("二次密码输入不一致")
            self.add_error("re_new_password", ValidationError("二次密码输入不一致"))

# 用户信息修改表单
class UserInfoChangeForm(forms.Form):
    SEX = ((0, '男'), (1, '女'))

    email = forms.EmailField(label="邮箱",
                             widget=forms.widgets.EmailInput(
                                 attrs={'class': 'form-control', }),
                             error_messages={
                                 'required': '邮箱不能为空',
                                 'invalid': '邮箱格式不对',
                             })
    true_name = forms.CharField(label="真实姓名", widget=forms.widgets.TextInput(
        attrs={'class': 'form-control', }))

    mobile = forms.CharField(label="手机号码", validators=[mobile_validate],
                             widget=forms.widgets.TextInput(
                                 attrs={'class': 'form-control', }),
                             error_messages={
                                 'required': '手机号码不能为空',
                             })

    sex = forms.ChoiceField(label='性别', choices=SEX, widget=forms.widgets.Select(attrs={"class": "form-control"}),
                            error_messages={
                                'required': '性别不能为空',
                            })

    user_img = forms.ImageField(label="用户头像", required=False, widget=forms.widgets.FileInput(
        attrs={'class': 'form-control'}))